﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Entity;
using System.Text;
using PBOffice.BLL;
using PBOffice.DAL;

public partial class PublicInformationModule_organisation : System.Web.UI.Page
{
    TreeViewManager tvManager = new TreeViewManager();
    List<v_DepOrgan> depList = new List<v_DepOrgan>();
    DepartmentManager depManager = new DepartmentManager();
    OrganManager organManager = new OrganManager();
    Department dep = new Department();
    UserManager userManager = new UserManager();
    //string nodeFormat = "<div>{0}</div><div><a style=\"text-decoration:none\" href=\"StaffFrameView.aspx?deptId=" + dept.DeptId + "\"><img border=\"0\" src=\"" + this.ResolveUrl("~/images/icon_users.gif") + "\"/>&nbsp;{1}人</a></div>";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ShowTreeNode();
        }
    }

    string getNode(int no,int parentNo,string text)      //机构节点信息的格式
    {
        return string.Format("myTree.add({0}, {1}, '{2}');", no, parentNo, text);
    }
    string getDepNode(int no,int parentNo,string text,int userCount) //部门节点信息的格式
    {
        return string.Format("myTree.add({0}, {1}, '{2}','../Images/icon_users.gif',{3},'人');", no, parentNo, text,userCount);
    }


    /// <summary>
    /// 显示架构图中各个节点的信息
    /// </summary>
    public void ShowTreeNode()
    {
        StringBuilder sb = new StringBuilder();
        sb.AppendFormat(getNode(0,-1,"公司架构图"));                    //添加公司架构图根节点信息
        List<Organ> organList = organManager.ShowAllOrganInfo();        //机构信息集合
        foreach( Organ organ in organList)
        {            
            sb.AppendFormat(getNode(organ.OrganNo,0,organ.OrganShortName)); //添加公司架构图一级子节点信息：机构节点
            depList = depManager.ShowDepInfoByOrganNo(organ.OrganNo);         //根据机构编号获取部门信息
            if(depList.Count>0)
            {
                foreach(v_DepOrgan dep in depList)
                {
                    List<v_UserDepPosition> userlist = userManager.ShowUserInfoByDep(dep.DepNo);  //根据部门编号查询用户信息
                    sb.AppendFormat(getDepNode(dep.DepNo,dep.OrganNo,dep.DepId,userlist.Count));   //节点信息
                    ShowChildNode(sb, dep);      //显示子节点信息
                }
            }
        }
    }
    /// <summary>
    /// 显示子节点信息
    /// </summary>
    /// <param name="sb">字符串流</param>
    /// <param name="dep">部门机构对象</param>
    private void ShowChildNode(StringBuilder sb, v_DepOrgan dep)
    {
        depList = depManager.GetSubDepartments(dep.DepNo);       //根据上级部门编号获取下级部门信息
        if (depList.Count > 0)
        {
            foreach (v_DepOrgan depChild in depList)
            {
                int parentNo = Convert.ToInt32(depChild.DepParentNo);
                sb.AppendFormat(getNode(depChild.DepNo, parentNo, dep.DepId));  //根据上级部门编号查询下级部门信息，并将其添加为该部门节点的子节点
                ShowChildNode(sb,depChild);                    //递归调用显示添加子节点信息
            }
        }
    }
   
}